//
//  WBHomeRetweetView.swift
//  WB123
//
//  Created by xuyunde mac pro on 2017/7/4.
//  Copyright © 2017年 NSObjiect. All rights reserved.
//

import UIKit
import SnapKit
class WBHomeRetweetView: UIView {

    // 记录转发微博的底部约束
    var retweetViewBottomConstraint: Constraint?
    // 提供属性供外界赋值
    var statusViewModel: WBStatusViewModel?{
        didSet{
            // 赋值
            contentLabel.text = statusViewModel?.homeModel?.retweeted_status?.text
            // 判断是否有配图
            /*
               配图用微博图片的类来做,
             1.加图控件,
             2.在didSet中判断是否有配图设置约束和隐藏,如果有 接口赋值(数组)给微博图的类的模型数组
            
             3.卸载约束
             */
            
            
            // 卸载约束
            retweetViewBottomConstraint?.deactivate()

            // y有配图
            if let pic_urls = statusViewModel?.homeModel?.retweeted_status?.pic_urls, pic_urls.count > 0{
                
                //赋值-----------把
                pictureView.picUrlsList = pic_urls
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                pictureView.isHidden = false
            }else{
                // 没配图
                snp.makeConstraints({ (make) in
                    retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                
                pictureView.isHidden = true
            }
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 设置视图
    private func setupUI(){
        backgroundColor = RGB(r: 235, g: 235, b: 235)
        // 添加控件
        addSubview(contentLabel)
        addSubview(pictureView)
        // 添加约束
        contentLabel.snp.makeConstraints { (make) in
            make.top.left.equalTo(10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
        }
        
        // 转发微博的底部约束
        snp.makeConstraints { (make) in
            retweetViewBottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
        }

    }
    
    
    //MARK: - 懒加载控件
    // 微博正文
    private lazy var contentLabel: UILabel = UILabel(text: "", textColor: UIColor.black, fontSize: NormalFontSize, maxWidth: SCREENW - 20)
    // 配图
    private lazy var pictureView: WBHomePictureView = {
        let view = WBHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
}
